package com.jsyxdl.utils;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hustcad.plm.pdm.attribute.model.dto.AttributeQueryDTO;
import com.hustcad.plm.pdm.attribute.service.TyplmBasicAttributeService;
import com.hustcad.plm.pdm.common.util.CommonUtil;
import com.hustcad.plm.pdm.ec.module.dto.ECObjDTO;
import com.hustcad.plm.pdm.ec.module.dto.ECObjData;
import com.hustcad.plm.pdm.ec.module.vo.ChgFormVO;
import com.hustcad.plm.pdm.enumeration.model.dto.EnumerationValueDTO;
import com.hustcad.plm.pdm.enumeration.model.vo.EnumerationItemHandleVO;
import com.hustcad.plm.pdm.enumeration.model.vo.EnumerationQueryVO;
import com.hustcad.plm.pdm.enumeration.service.TyplmEnumerationItemService;
import com.hustcad.plm.pdm.enumeration.service.TyplmEnumerationService;
import com.hustcad.plm.pdm.form.model.vo.FormTemplateVO;
import com.hustcad.plm.pdm.form.model.vo.TyplmFormDataVO;
import com.hustcad.plm.pdm.form.service.TyplmFormTemplateService;
import com.hustcad.plm.pdm.partbom.service.TyplmPartService;
import com.hustcad.plm.pdm.partbom.vo.PartDetailsVO;
import com.hustcad.plm.pdm.type.model.vo.TypeTree;
import com.hustcad.plm.pdm.type.service.TyplmTypeService;
import com.jsyxdl.entity.ProjectCodeDTO;
import com.jsyxdl.service.ProjectCodeService;
import com.ty.basic.annotation.BaseAttribute;
import com.ty.basic.dto.attribute.AttributeBaseDTO;
import com.ty.basic.entity.IdentifierEntity;
import com.ty.basic.entity.org.UserDO;
import com.ty.basic.vo.TyAttributeDefMapVO;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FormTemplateAndDataUtils {
    private static Map<String,Object> tyAttributeDefMap=new HashMap<>();
    private static TypeTree typeTree=null;
    private static Map<String,Map<String,String>> tyListDefMap=new HashMap<>();

    private static String proceData="\t\t{\n" +
            "\t\t\t\"key\": 120861,\n" +
            "\t\t\t\"type\": \"input\",\n" +
            "\t\t\t\"alias\": \"\",\n" +
            "\t\t\t\"icon\": \"danhangwenben\",\n" +
            "\t\t\t\"formItemFlag\": true,\n" +
            "\t\t\t\"options\": {\n" +
            "\t\t\t\t\"name\": \"input30152\",\n" +
            "\t\t\t\t\"keyNameEnabled\": false,\n" +
            "\t\t\t\t\"keyName\": \"\",\n" +
            "\t\t\t\t\"props\": \"\",\n" +
            "\t\t\t\t\"label\": \"项目号\",\n" +
            "\t\t\t\t\"labelAlign\": \"\",\n" +
            "\t\t\t\t\"contentAlign\": \"\",\n" +
            "\t\t\t\t\"type\": \"text\",\n" +
            "\t\t\t\t\"defaultValue\": \"\",\n" +
            "\t\t\t\t\"placeholder\": \"请输入\",\n" +
            "\t\t\t\t\"columnWidth\": \"200px\",\n" +
            "\t\t\t\t\"size\": \"\",\n" +
            "\t\t\t\t\"labelWidth\": 130,\n" +
            "\t\t\t\t\"colSpan\": 24,\n" +
            "\t\t\t\t\"labelHidden\": false,\n" +
            "\t\t\t\t\"labelWrap\": true,\n" +
            "\t\t\t\t\"readonly\": true,\n" +
            "\t\t\t\t\"clearable\": false,\n" +
            "\t\t\t\t\"disabled\": false,\n" +
            "\t\t\t\t\"hidden\": false,\n" +
            "\t\t\t\t\"operate\": \"\",\n" +
            "\t\t\t\t\"showPassword\": false,\n" +
            "\t\t\t\t\"required\": false,\n" +
            "\t\t\t\t\"requiredHint\": \"\",\n" +
            "\t\t\t\t\"isValidation\": false,\n" +
            "\t\t\t\t\"validation\": \"\",\n" +
            "\t\t\t\t\"validationHint\": \"\",\n" +
            "\t\t\t\t\"customClass\": [],\n" +
            "\t\t\t\t\"labelIconClass\": null,\n" +
            "\t\t\t\t\"labelIconPosition\": \"rear\",\n" +
            "\t\t\t\t\"labelTooltip\": null,\n" +
            "\t\t\t\t\"isLengthRange\": true,\n" +
            "\t\t\t\t\"minLength\": null,\n" +
            "\t\t\t\t\"maxLength\": null,\n" +
            "\t\t\t\t\"showWordLimit\": false,\n" +
            "\t\t\t\t\"prefixIcon\": \"\",\n" +
            "\t\t\t\t\"suffixIcon\": \"\",\n" +
            "\t\t\t\t\"appendButton\": false,\n" +
            "\t\t\t\t\"appendButtonDisabled\": false,\n" +
            "\t\t\t\t\"buttonIcon\": \"el-icon-search\",\n" +
            "\t\t\t\t\"onCreated\": \"\",\n" +
            "\t\t\t\t\"onMounted\": \"\",\n" +
            "\t\t\t\t\"onInput\": \"\",\n" +
            "\t\t\t\t\"onChange\": \"\",\n" +
            "\t\t\t\t\"onFocus\": \"\",\n" +
            "\t\t\t\t\"onBlur\": \"\",\n" +
            "\t\t\t\t\"onValidate\": \"\",\n" +
            "\t\t\t\t\"onAppendButtonClick\": \"\",\n" +
            "\t\t\t\t\"fontSize\": 12,\n" +
            "\t\t\t\t\"fontFamily\": \"verdana\"\n" +
            "\t\t\t},\n" +
            "\t\t\t\"id\": \"input30152\"\n" +
            "\t\t}";

    /**
     * 变更流程发起表单点击确定按钮保留变更单修改formData
     *
     * @param formVO
     */
    public static void createChangeNoticeByFormData(ChgFormVO formVO) {
        try {
            TyplmFormTemplateService typlmFormTemplateService = SpringUtil.getBean(TyplmFormTemplateService.class);
            TyplmPartService typlmPartService = SpringUtil.getBean(TyplmPartService.class);
            FormTemplateVO formTemplateVO = typlmFormTemplateService.getFormTemplateByType(formVO.getTypeOid());
            JSONObject formTemplate = formTemplateVO.getTemplatedata();
            JSONArray widgetList = formTemplate.getJSONArray("widgetList");

            //table表列定义
            JSONArray tableColumns = null;

            for (int i = 0; i < widgetList.size(); i++) {
                JSONObject widget = widgetList.getJSONObject(i);
                String type = widget.getString("type");
                if ("data-table".equals(type)) {
                    JSONObject options = widget.getJSONObject("options");
                    tableColumns = options.getJSONArray("tableColumns");
                }
            }

            if (tableColumns != null) {
                JSONObject formData = JSONObject.parseObject(formVO.getFormData());
                JSONArray datatableb = new JSONArray();

                List<ECObjDTO> ecObjectList = formVO.getEcObjectList();
                for (int b = 0; b < ecObjectList.size(); b++) {
                    BigInteger oid = ecObjectList.get(b).getOid();
                    String oType = ecObjectList.get(b).getOtype();
                    PartDetailsVO partDetailsVO = typlmPartService.queryPartDetails(oid, oType);
                    JSONObject addObj = new JSONObject();
                    addObj.put("_X_ROW_KEY", String.format("row_%d", 1 + b));
                    for (int a = 0; a < tableColumns.size(); a++) {
                        String field = tableColumns.getJSONObject(a).getString("field");
                        Object fieldValue = getFieldValue(field, partDetailsVO);
                        if (fieldValue == null) {
                            Map<String, List<String>> ibaAttributeMap = partDetailsVO.getIbaAttributeMap();
                            if (ibaAttributeMap != null) {
                                if (ibaAttributeMap.containsKey(field)) {
                                    List<String> valList = (List<String>) ibaAttributeMap.get(field);
                                    if (valList != null && valList.size() > 0) {
                                        fieldValue = valList.get(0);
                                    }
                                }
                            }
                        }
                        addObj.put(field, fieldValue);
                    }
                    datatableb.add(addObj);
                }
                formData.put("datatableb1", datatableb);
                formVO.setFormData(formData.toJSONString());
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    /**
     * 变更流程流程过程中修改表单值
     *
     * @param formVO
     */
    public static boolean updateChgFormByFormData(ChgFormVO formVO) {
        try {
            TyplmFormTemplateService typlmFormTemplateService = SpringUtil.getBean(TyplmFormTemplateService.class);
            TyplmPartService typlmPartService = SpringUtil.getBean(TyplmPartService.class);
            FormTemplateVO formTemplateVO = typlmFormTemplateService.getFormTemplateByType(formVO.getTypeOid());
            JSONObject formTemplate = formTemplateVO.getTemplatedata();
            JSONObject formData = JSONObject.parseObject(formVO.getFormData());

            JSONArray widgetList = formTemplate.getJSONArray("widgetList");

            for (int i = 0; i < widgetList.size(); i++) {
                JSONObject widget = widgetList.getJSONObject(i);
                String type = widget.getString("type");
                if ("data-table".equals(type)) {
                    JSONObject options = widget.getJSONObject("options");
                    JSONArray tableColumns = options.getJSONArray("tableColumns");
                    if (tableColumns != null) {
                        JSONArray datatablebOld = formData.getJSONArray("datatableb1");
                        Map<Integer, Object> cmdMap = new HashMap<>();
                        for (Integer a = 0; a < datatablebOld.size(); a++) {
                            if (datatablebOld.getJSONObject(a).containsKey("_X_ROW_KEY")) {
                                cmdMap.put(a, datatablebOld.getJSONObject(a).getString("_X_ROW_KEY"));
                            }
                        }


                        JSONArray datatableb = new JSONArray();

                        List<ECObjData> ecObjDatatList = formVO.getEcObjDataVOS();
                        for (Integer b = 0; b < ecObjDatatList.size(); b++) {
                            BigInteger oid = ecObjDatatList.get(b).getOid();
                            String oType = ecObjDatatList.get(b).getOtype();
                            PartDetailsVO partDetailsVO = typlmPartService.queryPartDetails(oid, oType);
                            JSONObject addObj = new JSONObject();
                            if (cmdMap.containsKey(b)) {
                                addObj.put("_X_ROW_KEY", cmdMap.get(b));
                            } else {
                                addObj.put("_X_ROW_KEY", String.format("row_%d", cmdMap.size() + b + 1));
                            }
                            for (int a = 0; a < tableColumns.size(); a++) {
                                String field = tableColumns.getJSONObject(a).getString("field");
                                Object fieldValue = getFieldValue(field, partDetailsVO);
                                if (fieldValue == null) {
                                    Map<String, List<String>> ibaAttributeMap = partDetailsVO.getIbaAttributeMap();
                                    if (ibaAttributeMap != null) {
                                        if (ibaAttributeMap.containsKey(field)) {
                                            List<String> valList = (List<String>) ibaAttributeMap.get(field);
                                            if (valList != null && valList.size() > 0) {
                                                fieldValue = valList.get(0);
                                            }
                                        }
                                    }
                                }
                                addObj.put(field, fieldValue);
                            }
                            datatableb.add(addObj);
                        }
                        formData.put("datatableb1", datatableb);
                        formVO.setFormData(formData.toJSONString());
                        return true;
                    }
                }
            }
            for(Map.Entry<String,Object> entity:formData.getInnerMap().entrySet()){
                String itemKey=entity.getKey();
                if(tyListDefMap.containsKey(itemKey)){
                    Map<String,String> mapperMap=tyListDefMap.get(itemKey);
                    JSONArray table=new JSONArray();
                    JSONArray itemList= (JSONArray) entity.getValue();
                    JSONObject item=itemList.getJSONObject(0);
                    String jsonStr=item.toJSONString();
                    List<ECObjData> ecObjDatatList = formVO.getEcObjDataVOS();
                    for (Integer b = 0; b < ecObjDatatList.size(); b++) {
                        BigInteger oid = ecObjDatatList.get(b).getOid();
                        String oType = ecObjDatatList.get(b).getOtype();
                        PartDetailsVO partDetailsVO = typlmPartService.queryPartDetails(oid, oType);
                        JSONObject obj=JSONObject.parseObject(new String(jsonStr));
                        Map<String,Object> addMap=new HashMap<>();
                        for(Map.Entry<String,Object> entityMap:obj.getInnerMap().entrySet()){
                            String entityKey=entityMap.getKey();
                            if(entityKey.indexOf("@") != -1){
                                entityKey=entityKey.substring(0,entityKey.indexOf("@"));
                            }
                            if(mapperMap.containsKey(entityKey)){
                                String field=mapperMap.get(entityKey);
                                Object fieldValue = getFieldValue(field, partDetailsVO);
                                if (fieldValue == null) {
                                    Map<String, List<String>> ibaAttributeMap = partDetailsVO.getIbaAttributeMap();
                                    if (ibaAttributeMap != null) {
                                        if (ibaAttributeMap.containsKey(field)) {
                                            List<String> valList = (List<String>) ibaAttributeMap.get(field);
                                            if (valList != null && valList.size() > 0) {
                                                fieldValue = valList.get(0);
                                            }
                                        }
                                    }
                                }
                                addMap.put(entityKey,fieldValue);
                            }
                        }

                        obj.putAll(addMap);

                        table.add(obj);
                    }
                    formData.put(entity.getKey(),table);
                }
            }
            formVO.setFormData(formData.toJSONString());
        }catch (Exception e){
            e.printStackTrace();
        }


        return false;
    }


    /**
     * 选择变更类型页面下一步刷新表单处理
     *
     * @param formTemplateVO
     * @param json
     */
    public static void getFormTemplateByTypeByFormData(FormTemplateVO formTemplateVO, Object json) {
        try {
            TyplmPartService typlmPartService=SpringUtil.getBean(TyplmPartService.class);

            Map<String, Object> map = (Map<String, Object>) json;


            List<Map<String, String>> hider = getPropertiesHide();


            Map<String, String> mapper = getPropertiesMapper();

            Map<String, Object> parameters = (Map<String, Object>) map.get("parameters");
            Map<String, Object> data = (Map<String, Object>) parameters.get("data");


            JSONObject formTemplate = formTemplateVO.getTemplatedata();
            JSONArray widgetList = formTemplate.getJSONArray("widgetList");
            boolean isWrite=false;
            for (int i = 0; i < widgetList.size(); i++) {
                JSONObject widget = widgetList.getJSONObject(i);
                JSONObject cmdOptions = widget.getJSONObject("options");

                if (hider != null) {
                    for (int a = 0; a < hider.size(); a++) {
                        Map<String, String> hide = hider.get(a);
                        boolean isHides[] = new boolean[hide.size()];
                        int index = 0;
                        for (Map.Entry<String, String> entity : hide.entrySet()) {
                            String cmd = widget.getString(entity.getKey());
                            String cmdText = cmdOptions.getString(entity.getKey());
                            if (cmd != null) {
                                if (cmd.equals(entity.getValue())) {
                                    isHides[index] = true;
                                }
                            }
                            if (cmdText != null) {
                                if (cmdText.equals(entity.getValue())) {
                                    isHides[index] = true;
                                }
                            }
                            index++;
                        }
                        boolean isHide = true;
                        for (int b = 0; b < isHides.length; b++) {
                            if (isHides[b] != true) {
                                isHide = false;
                                break;
                            }
                        }
                        if (isHide) {
                            cmdOptions.put("hidden", true);
                        }
                    }

                }


                String type = widget.getString("type");
                if ("data-table".equals(type)) {
                    JSONArray tableColumns = cmdOptions.getJSONArray("tableColumns");
                    if (tableColumns != null) {
                        String name = cmdOptions.getString("name");
                        if (mapper.containsKey(name)) {
                            String listName = (String) mapper.get(name);
                            if (parameters.containsKey(listName)) {
                                List<Map<String, Object>> dataTableList = (List<Map<String, Object>>) parameters.get(
                                        listName);
                                if (dataTableList != null) {
                                    JSONArray datatableb = new JSONArray();
                                    for (Integer b = 0; b < dataTableList.size(); b++) {
                                        Map<String, Object> dataMap = dataTableList.get(b);

                                        BigInteger oid=new BigInteger(String.valueOf(dataMap.get("oid")));
                                        String otype= (String) dataMap.get("otype");
                                        PartDetailsVO partDetails = typlmPartService.queryPartDetails(oid, otype);
                                        Map<String,Object> typeKeyMap=getTypeOptionsToMap(partDetails.getTypeName());
                                        if(typeKeyMap!=null){
                                            List<TyAttributeDefMapVO> attrDefMapList= (List<TyAttributeDefMapVO>) typeKeyMap.get("attrDefMapList");
                                            List<AttributeBaseDTO> attributeBaseList= (List<AttributeBaseDTO>) typeKeyMap.get("attributeBaseList");
                                            //常规属性
                                            Map<String,String> arrtBute=new HashMap<>();
                                            for(AttributeBaseDTO attributeBaseDTO: attributeBaseList){
                                                arrtBute.put(attributeBaseDTO.getDisplayName(),attributeBaseDTO.getName());
                                            }
                                            //扩展属性
                                            Map<String,String> attrDef=new HashMap<>();
                                            for(TyAttributeDefMapVO tyAttributeDefMapVO: attrDefMapList){
                                                attrDef.put(tyAttributeDefMapVO.getDisplayName(),tyAttributeDefMapVO.getName());
                                            }


                                            JSONObject addObj = new JSONObject();
                                            for (int a = 0; a < tableColumns.size(); a++) {
                                                String field = tableColumns.getJSONObject(a).getString("field");
                                                Object fieldValue=null;
                                                if(arrtBute.containsKey(field)){
                                                    fieldValue = getFieldValue(arrtBute.get(field),partDetails);
                                                }

                                                if (fieldValue == null) {
                                                    Map<String, List<String>> ibaAttributeMap = partDetails.getIbaAttributeMap();

                                                    if (ibaAttributeMap != null) {
                                                        if(attrDef.containsKey(field)){
                                                            List<String> strs = ibaAttributeMap.get(attrDef.get(field));
                                                            if (strs != null && strs.size() > 0) {
                                                                fieldValue = strs.get(0);
                                                            }
                                                        }
                                                    }
                                                }
                                                if(fieldValue==null){
                                                    if(arrtBute.containsKey(field)==false){
                                                        fieldValue = getFieldValue(field,partDetails);
                                                    }
                                                }
                                                addObj.put(field, fieldValue);
                                            }
                                            datatableb.add(addObj);
                                        }

                                    }

                                    cmdOptions.put("tableData", datatableb);
                                }
                            }
                        }
                    }

                }
                else if ("grid-sub-form".equals(type)) {
                    String name = cmdOptions.getString("name");
                    String listName = (String) mapper.get(name);
                    if(tyListDefMap.containsKey(name)){
                        isWrite=true;
                        Map<String,String> mapDef=tyListDefMap.get(name);
                        JSONArray childWidgetList1 = widget.getJSONArray("widgetList");
                        JSONObject childObj=childWidgetList1.getJSONObject(0);
                        String jsonStr=new String(childObj.toJSONString());
                        JSONArray newList=new JSONArray();
                        List<Map<String, Object>> dataTableList = (List<Map<String, Object>>) parameters.get(listName);
                        for(int h=0;h<dataTableList.size();h++){
                            Map<String, Object> dataMap = dataTableList.get(h);
                            BigInteger oid=new BigInteger(String.valueOf(dataMap.get("oid")));
                            String otype= (String) dataMap.get("otype");
                            PartDetailsVO partDetails = typlmPartService.queryPartDetails(oid, otype);

                            JSONObject add=JSONObject.parseObject(new String(jsonStr));

                            JSONArray cols=add.getJSONArray("cols");
                            for(int y=0;y<cols.size();y++){
                                JSONArray childWidgetList=cols.getJSONObject(y).getJSONArray("widgetList");
                                for(int z=0;z<childWidgetList.size();z++){
                                    if(childWidgetList.getJSONObject(z).containsKey("rows")){
                                        JSONArray rows = childWidgetList.getJSONObject(z).getJSONArray("rows");
                                        for(int x=0;x<rows.size();x++){
                                            JSONArray rowsCols=rows.getJSONObject(x).getJSONArray("cols");
                                            for(int c=0;c<rowsCols.size();c++){
                                                JSONArray rowsColsWidgetList=rowsCols.getJSONObject(c).getJSONArray("widgetList");
                                                for(int d=0;d<rowsColsWidgetList.size();d++){
                                                    JSONObject itemOptions=rowsColsWidgetList.getJSONObject(d).getJSONObject("options");
                                                    String key=itemOptions.getString("name");
                                                    String field=mapDef.get(key);
                                                    if(mapDef.containsKey(key)){
                                                        Object fieldValue=getFieldValue(field,partDetails);
                                                        if (fieldValue == null) {
                                                            Map<String, List<String>> ibaAttributeMap = partDetails.getIbaAttributeMap();
                                                            if (ibaAttributeMap != null) {
                                                                if (ibaAttributeMap.containsKey(field)) {
                                                                    List<String> valList = (List<String>) ibaAttributeMap.get(field);
                                                                    if (valList != null && valList.size() > 0) {
                                                                        fieldValue = valList.get(0);
                                                                    }
                                                                }
                                                            }
                                                        }
                                                        itemOptions.put("defaultValue",fieldValue);
                                                        String itemId=rowsColsWidgetList.getJSONObject(d).getString("id");
                                                        rowsColsWidgetList.getJSONObject(d).put("id",itemId+"@"+h);
                                                        itemOptions.put("name",itemId+"@"+h);
                                                    }
                                                }
                                            }

                                        }
                                    }
                                }

                            }
                            newList.add(add);
                        }

                        widget.put("widgetList",newList);
                    }
                }
                else if ("static-text".equals(type)) {
                }
                else {
                    JSONArray rows = widget.getJSONArray("rows");
                    if (rows != null) {
                        for (int a = 0; a < rows.size(); a++) {
                            JSONArray cols = rows.getJSONObject(a).getJSONArray("cols");
                            for (int b = 0; b < cols.size(); b++) {
                                JSONArray widgetListCols = cols.getJSONObject(b).getJSONArray("widgetList");
                                for (int c = 0; c < widgetListCols.size(); c++) {
                                    JSONObject options = widgetListCols.getJSONObject(c).getJSONObject("options");
                                    if (options != null) {
                                        String name = options.getString("name");
                                        if (mapper.containsKey(name)) {
                                            options.put("defaultValue", data.get(mapper.get(name)));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if(isWrite==false){
                JSONObject widget = getWidgetList(widgetList);
                if(widget!=null){
                    JSONObject cmdOptions = widget.getJSONObject("options");
                    String name = cmdOptions.getString("name");
                    String listName = (String) mapper.get(name);
                    if(tyListDefMap.containsKey(name)){
                        Map<String,String> mapDef=tyListDefMap.get(name);
                        JSONArray childWidgetList1 = widget.getJSONArray("widgetList");
                        JSONObject childObj=childWidgetList1.getJSONObject(0);
                        String jsonStr=new String(childObj.toJSONString());
                        JSONArray newList=new JSONArray();
                        List<Map<String, Object>> dataTableList = (List<Map<String, Object>>) parameters.get(listName);
                        for(int h=0;h<dataTableList.size();h++){
                            Map<String, Object> dataMap = dataTableList.get(h);
                            BigInteger oid=new BigInteger(String.valueOf(dataMap.get("oid")));
                            String otype= (String) dataMap.get("otype");
                            PartDetailsVO partDetails = typlmPartService.queryPartDetails(oid, otype);

                            JSONObject add=JSONObject.parseObject(new String(jsonStr));

                            JSONArray cols=add.getJSONArray("cols");
                            for(int y=0;y<cols.size();y++){
                                JSONArray childWidgetList=cols.getJSONObject(y).getJSONArray("widgetList");
                                for(int z=0;z<childWidgetList.size();z++){
                                    JSONObject itemOptions=childWidgetList.getJSONObject(z).getJSONObject("options");
                                    String key=itemOptions.getString("name");
                                    if(mapDef.containsKey(key)){
                                        String field=mapDef.get(key);
                                        Object fieldValue=getFieldValue(field,partDetails);
                                        if (fieldValue == null) {
                                            Map<String, List<String>> ibaAttributeMap = partDetails.getIbaAttributeMap();
                                            if (ibaAttributeMap != null) {
                                                if (ibaAttributeMap.containsKey(field)) {
                                                    List<String> valList = (List<String>) ibaAttributeMap.get(field);
                                                    if (valList != null && valList.size() > 0) {
                                                        fieldValue = valList.get(0);
                                                    }
                                                }
                                            }
                                        }
                                        itemOptions.put("defaultValue",fieldValue);
                                        String itemId=childWidgetList.getJSONObject(z).getString("id");
                                        childWidgetList.getJSONObject(z).put("id",itemId+"@"+h);
                                        itemOptions.put("name",itemId+"@"+h);
                                    }
                                }

                            }

                            newList.add(add);
                        }

                        widget.put("widgetList",newList);
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }


    /**
     * 项目号申请流程走完后表单中显示项目号
     * @param formDataVO
     */
    public static void getFormDataHasContainerByOid(TyplmFormDataVO formDataVO) {
        try {
            if("项目申请表单".equals(formDataVO.getTypeDefName())==false){
                return;
            }
            String formTemplateStr = formDataVO.getTemplatedata();
            JSONObject formTemplate=JSONObject.parseObject(formTemplateStr);


            JSONObject formData=formDataVO.getFormdata();
            String projectName=formData.getString("projectName");
            if("".equals(projectName)){
                return;
            }

            JSONArray widgetList = formTemplate.getJSONArray("widgetList");
            boolean isAdd=true;
            for (int i = 0; i < widgetList.size(); i++) {
                JSONObject widget = widgetList.getJSONObject(i);
                JSONObject cmdOptions = widget.getJSONObject("options");
                if("项目号".equals(cmdOptions.getString("label"))){
                    isAdd=false;
                    break;
                }
            }

            if(isAdd){
                TyplmEnumerationService typlmEnumerationService = SpringUtil.getBean(TyplmEnumerationService.class);
                EnumerationQueryVO enumerationQueryVO = new EnumerationQueryVO();
                enumerationQueryVO.setSystem(0);

                EnumerationValueDTO projec = null;
                List<EnumerationValueDTO> enumList = typlmEnumerationService.getEnumerationDefList(enumerationQueryVO);
                for (int i = 0; i < enumList.size(); i++) {
                    if ("项目编号".equals(enumList.get(i).getName())) {
                        projec = enumList.get(i);
                    }
                }
                String projectNumber="";
                if(projec!=null){
                    TyplmEnumerationItemService typlmEnumerationItemService=SpringUtil.getBean(TyplmEnumerationItemService.class);
                    IdentifierEntity identifierEntity=new IdentifierEntity();
                    identifierEntity.setOid(projec.getOid());
                    identifierEntity.setOtype(projec.getOtype());
                    List<EnumerationItemHandleVO> list=typlmEnumerationItemService.queryEnumerationItemByDefOid(identifierEntity);
                    for(int a=0;a<list.size();a++){
                        if(projectName.equals(list.get(a).getDisplayName())){
                            projectNumber=list.get(a).getName();
                            break;
                        }
                    }
                }
                JSONObject addRows=JSONObject.parseObject(proceData);
                JSONObject options = addRows.getJSONObject("options");
                options.put("defaultValue",projectNumber);
                widgetList.add(1,addRows);
                Field field=formDataVO.getClass().getDeclaredField("templatedata");
                field.setAccessible(true);
                field.set(formDataVO,formTemplate.toJSONString());
            }
            else{
                TyplmEnumerationService typlmEnumerationService = SpringUtil.getBean(TyplmEnumerationService.class);
                EnumerationQueryVO enumerationQueryVO = new EnumerationQueryVO();
                enumerationQueryVO.setSystem(0);

                EnumerationValueDTO projec = null;
                List<EnumerationValueDTO> enumList = typlmEnumerationService.getEnumerationDefList(enumerationQueryVO);
                for (int i = 0; i < enumList.size(); i++) {
                    if ("项目编号".equals(enumList.get(i).getName())) {
                        projec = enumList.get(i);
                    }
                }
                String projectNumber="";
                if(projec!=null){
                    TyplmEnumerationItemService typlmEnumerationItemService=SpringUtil.getBean(TyplmEnumerationItemService.class);
                    IdentifierEntity identifierEntity=new IdentifierEntity();
                    identifierEntity.setOid(projec.getOid());
                    identifierEntity.setOtype(projec.getOtype());
                    List<EnumerationItemHandleVO> list=typlmEnumerationItemService.queryEnumerationItemByDefOid(identifierEntity);
                    for(int a=0;a<list.size();a++){
                        if(projectName.equals(list.get(a).getDisplayName())){
                            projectNumber=list.get(a).getName();
                            break;
                        }
                    }
                }

                for (int i = 0; i < widgetList.size(); i++) {
                    JSONObject widget = widgetList.getJSONObject(i);
                    JSONObject cmdOptions = widget.getJSONObject("options");
                    if("项目号".equals(cmdOptions.getString("label"))){
                        String key=cmdOptions.getString("name");
                        formData.put(key,projectNumber);
                        Field field=formDataVO.getClass().getDeclaredField("formdata");
                        field.setAccessible(true);
                        field.set(formDataVO,formData.toJSONString());
                        break;
                    }
                }
            }

        }catch (Exception e){
            e.printStackTrace();
        }

    }



    //获取类型属性值
    private static Object getFieldValue(String nameStr, PartDetailsVO partDetailsVO) {
        try {
            Field field = partDetailsVO.getClass().getDeclaredField(nameStr);
            if (field != null) {
                field.setAccessible(true);
                return field.get(partDetailsVO);
            }

        } catch (Exception e) {
            Field fields[] = partDetailsVO.getClass().getDeclaredFields();
            for(int i=0;i<fields.length;i++){
                BaseAttribute annotations=fields[i].getAnnotation(BaseAttribute.class);
                if(annotations!=null){
                    if(annotations.name().equals(nameStr)){
                        fields[i].setAccessible(true);
                        try {
                            return fields[i].get(partDetailsVO);
                        } catch (Exception ex) {
                        }
                    }
                }
            }
        }
        return null;
    }


    /**
     * 解析properties文件组织成结构
     * @return
     * @throws UnsupportedEncodingException
     */
    private static List<Map<String, String>> getPropertiesHide() {
        List<Map<String, String>> list=new ArrayList<>();
        String hiderStr = null;
        try {
            hiderStr = new String(
                    SpringUtil.getProperty("formData.hider").getBytes("ISO-8859-1"), "UTF-8");
        } catch (Exception e) {
        }
        if(hiderStr!=null){
            String sub[] = hiderStr.split(";");
            for(int i=0;i<sub.length;i++){
                String listkey[]=sub[i].split(",");
                Map<String,String> map=new HashMap<>();
                for(int a=0;a<listkey.length;a++){
                    String mapkey[]=listkey[a].split(":");
                    if(mapkey.length==2){
                        map.put(mapkey[0],mapkey[1]);
                    }
                }
                if(map.size()>0){
                    list.add(map);
                }
            }
        }

        return list;
    }

    private static Map<String, String> getPropertiesMapper() {
        Map<String, String> mapper=new HashMap<>();
        String mapperStr = null;
        try {
            mapperStr = new String(
                    SpringUtil.getProperty("formData.mapper").getBytes("ISO-8859-1"), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        if(mapperStr!=null){
            String listkey[]=mapperStr.split(",");
            for(int a=0;a<listkey.length;a++){
                String mapkey[]=listkey[a].split(":");
                if(mapkey.length==2){
                    if(mapkey[1].indexOf("-")!= -1){
                        String listName=mapkey[1].substring(0,mapkey[1].indexOf("-"));
                        mapper.put(mapkey[0],listName);

                        Map<String,String> lkey=null;
                        if(tyListDefMap.containsKey(mapkey[0])){
                            lkey=tyListDefMap.get(mapkey[0]);
                        }
                        else{
                            lkey=new HashMap<>();
                            tyListDefMap.put(mapkey[0],lkey);
                        }
                        String arr[]=mapkey[1].split("-");
                        if(arr.length==2){
                            String mStr=arr[1].substring(1,arr[1].length()-1);
                            String keys[]=mStr.split(";");
                            for(int i=0;i<keys.length;i++){
                                String arrlist[]=keys[i].split("=");
                                if(arrlist.length==2){
                                    lkey.put(arrlist[0],arrlist[1]);
                                }
                            }
                        }
                    }
                    else{
                        mapper.put(mapkey[0],mapkey[1]);
                    }
                }
            }
        }

        return mapper;
    }

    /**
     * 获取类型属性的分类属性
     * @param typeDefName
     * @return
     */
    private static Map<String,Object> getTypeOptionsToMap(String typeDefName){
        TyplmTypeService typlmTypeService=SpringUtil.getBean(TyplmTypeService.class);
        TyplmBasicAttributeService typlmBasicAttributeService=SpringUtil.getBean(TyplmBasicAttributeService.class);
        if(typeTree==null){
            List<TypeTree> typeTrees = typlmTypeService.queryTypeTree();
            for(int i=0;i<typeTrees.size();i++){
                if("零部件".equals(typeTrees.get(i).getDisplayName())){
                    typeTree=typeTrees.get(i);
                    break;
                }
            }
        }

        if(typeTree!=null){
            for(int i=0;i<typeTree.getChildList().size();i++){
                if(typeDefName.equals(typeTree.getChildList().get(i).getName())){
                    if(tyAttributeDefMap.containsKey(typeTree.getChildList().get(i).getName())==false){
                        List<TyAttributeDefMapVO> attrDefMapList = typlmTypeService.queryInheritAttrDef(typeTree.getChildList().get(i).getOid(), typeTree.getChildList().get(i).getOtype());
                        AttributeQueryDTO queryDTO=new AttributeQueryDTO();
                        queryDTO.setNeedHardAttribute(true);
                        queryDTO.setNeedIBAAttribute(false);
                        queryDTO.setNeedVirtualAttribute(true);
                        queryDTO.setTypeoid(typeTree.getChildList().get(i).getOid());
                        queryDTO.setTypeotype(typeTree.getChildList().get(i).getOtype());
                        List<AttributeBaseDTO> result = typlmBasicAttributeService.queryInheritAttributeDef(queryDTO);
                        Map<String,Object> map=new HashMap<>();
                        map.put("attrDefMapList",attrDefMapList);
                        map.put("attributeBaseList",result);
                        tyAttributeDefMap.put(typeTree.getChildList().get(i).getName(),map);
                        return map;
                    }
                    else{
                        Map<String,Object> map= (Map<String, Object>) tyAttributeDefMap.get(typeTree.getChildList().get(i).getName());
                        return map;
                    }
                }
            }
        }

        return null;
    }

    private static JSONObject getWidgetList(JSONArray widgetList){
        for (int i = 0; i < widgetList.size(); i++){
            JSONObject widget = widgetList.getJSONObject(i);
            JSONObject cmdOptions = widget.getJSONObject("options");
            String type = widget.getString("type");
            if ("grid-sub-form".equals(type)){
                String name = cmdOptions.getString("name");
                if(tyListDefMap.containsKey(name)){
                    return widget;
                }
            }
            if(widget.containsKey("cols")){
                JSONArray cols=widget.getJSONArray("cols");
                for(int y=0;y<cols.size();y++){
                    JSONArray childwidgetList=cols.getJSONObject(y).getJSONArray("widgetList");
                    return getWidgetList(childwidgetList);
                }
            }
        }
        return null;
    }
}
